<?php
/**
 * Created by PhpStorm.
 * User: 陈艺赫
 * Date: 2017/12/11
 * Time: 2:20
 */

namespace Home\Controller;


class QmlistController extends BaseController
{
    public function index()
    {
        if (empty(session('guest'))) {
            $this->success('请先登录!');
            exit;
        }
        if (IS_GET) {
            $qm = I('get.qm');
            $this->assign('qm', $qm);
        }
        $this->display();
    }

    public function dataGet()
    {
        $db_nametxt = M('nametxt');
        $sum = $db_nametxt->where('count = 2 and status = "1"')->Sum('hits');
        $data_nametxt = $db_nametxt->where('count = 2 and status = "1" and hits > 0')->select();
        $data_ids = array_column($data_nametxt, 'id');
        foreach ($data_nametxt as $value) {
            if ($value['hits'] > 1) {
                for ($i = 1; $i < $value['hits']; $i++) {
                    $data_ids[] = $value['id'];
                }
            }
        }
        $data_list = [];
        $i=0;
        while($i < 10) {
            $ran = rand(0, $sum - 1);
            if (!in_array($data_ids[$ran], $data_list)) {
                $data_list[] = $data_ids[$ran];
                $i++;
            }
        }
        $map['id'] = array('in',$data_list);
        $data = $db_nametxt->where($map)->select();
        $this->ajaxReturn($data);
    }
}